/**
 *
 */
package cardval.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cardval.application.BankInfoCheck;
import cardval.dao.CardInfoDao;
import cardval.dao.CardTransLogDao;
import cardval.dao.PartnerDao;
import cardval.entity.CardInfo;
import cardval.entity.CardInfoTransLog;
import cardval.entity.PartnerInfo;
import cardval.entity.PartnerTrans;
import cardval.entity.Result;
import cardval.util.Constant;
import cardval.util.DateUtil;
import cardval.util.StringUtil;

/**
 * @author Renne66
 * @Description: 服务类
 * @email 465119455@qq.com
 * @date 2016年4月21日
 */
@Component
public class SystemService {

    private static final Logger logger = Logger.getLogger(SystemService.class);
    @Autowired
    private CardInfoDao cardInfoDao;
    @Autowired
    private CardTransLogDao cardTransLogDao;
    @Autowired
    private PartnerDao partnerDao;

    private static final List<String> codeList = new ArrayList<>();

    static {
        codeList.add("01");
    }

    /**
     * 查询合作商
     *
     * @return
     */
    public List<PartnerInfo> getPartnerInfo(String partnerNo, int offset, int limit) {
        return partnerDao.queryPartners(partnerNo, offset, limit);
    }

    public List<PartnerInfo> getPartnerInfo(String partnerNo) {
        return partnerDao.queryPartners(partnerNo);
    }

    /**
     * 查询合作商的交易记录
     *  @param partnerId 合作商ID
     *                  //	 * @param partnerType 交易类型 1-充值 2-消费
     * @param transType
     * @param offset limit(offset, limit)
     * @param limit limit(offset, limit)
     */
    public List<PartnerTrans> getPartnerTrans(String partnerId, String transType, Integer limit, Integer offset) {
        return partnerDao.queryPartnerTrans(partnerId, transType, offset, limit);

    }

    /**
     * 添加合作商
     *
     * @param partnerInfo
     */
    public int addPartnerInfo(PartnerInfo partnerInfo) {
        return partnerDao.addPartner(partnerInfo);
    }

    /**
     * 合作商交易
     *
     * @param partnerInfo 合作商信息
     * @param money       交易金额
     * @param transType   交易类型  1 充值 2 消费
     * @return
     */
    
    public void transPartnerMoney(PartnerInfo partnerInfo, int money, int transType) {
        //添加合作伙伴消费信息
        PartnerTrans partnerTrans = new PartnerTrans();
        partnerTrans.setPARTNER_ID_(partnerInfo.getID_());
        partnerTrans.setTRANS_TYPE_(transType);
        partnerTrans.setPartnerNo(partnerInfo.getPARTNER_NO_());
        partnerTrans.setTRANS_MONEY_(money);
        
        partnerDao.addPartnerTrans(partnerTrans);
//        int result = transType == 1 ? partnerInfo.getPARTNER_MONEY_() + money : partnerInfo.getPARTNER_MONEY_() - money;
        //更新合作伙伴余额需要在数据库里面进行更新操作的
        partnerDao.updatePartnersMoney(partnerInfo.getPARTNER_NO_(), money, transType);
        //日志输出
        if(transType==2) {
        int partner_money = partnerInfo.getPARTNER_MONEY_()- money;//显示的话默认显示原来的
        logger.info("------合作商的money---------"+partner_money);
        }else{
        	int partner_money = partnerInfo.getPARTNER_MONEY_()+ money;//显示的话默认显示原来的
        	logger.info("------合作商的money---------"+partner_money);
        }
    }
    
    //全部显示卡片的信息并且是能够查询的----------- 
    /*public List<CardInfo> getCardInfos(String cardNo, int offset, int limit) {
        return cardInfoDao.queryCardInfo(new CardInfo(cardNo), offset, limit);
    }*/
    //这里是做了更改的20170830
    //新增的更改再可以根据流水号进行查询
    public List<CardInfo> getCardInfos(String serialNo,String partnerNo, String cardNo,String cardName, String cardMobile, String cardIdcard, int limit, int offset) {
        return cardInfoDao.queryCardInfo(serialNo, partnerNo, cardNo, cardName, cardMobile, cardIdcard, limit, offset);
    }
    public List<CardInfo> getCardInfos(String serialNo,String partnerNo, String cardNo,String cardName, String cardMobile, String cardIdcard) {
        return cardInfoDao.queryCardInfo(serialNo,partnerNo, cardNo, cardName, cardMobile, cardIdcard);
    }

    public int delCardInfos(String cardId) {
        return cardInfoDao.delCardInfo(cardId);
    }

    public int addCardInfo(CardInfo paramsCard) {
        return cardInfoDao.addCardInfo(paramsCard);
    }
    /**
     * 交易日志添加到数据库中 0914
     */
    public int addCardTransLog(CardInfoTransLog paramsCard) {
    	return cardTransLogDao.addCardTransLog(paramsCard);
    }
    
    
    
    
    

    /**
     * 银行卡账户验证
     *
     * @return 0 成功  -1 失败
     */
    
    public  Result validation(Date st1Date,CardInfo paramsCard, String partnerNo) {
    	
        //查询合作商是否存在及余额是否充足
        List<PartnerInfo> partnerInfos = partnerDao.queryPartners(partnerNo);
        if (partnerInfos.size() == 0) {     	
        	return new Result("10", "合作商不存在");
        }

        PartnerInfo partnerInfo = partnerInfos.get(0);
        if (partnerInfo.getPARTNER_MONEY_() < Constant.BANK_PRICE)
            return new Result("10", "合作商余额不足", partnerInfo.getPARTNER_MONEY_());
        //不在本地进行查询信息是否存在的，直接调用远程接口查询的
        /*//查询卡片信息是否在数据库中存在
        List<CardInfo> cardInfos = cardInfoDao.queryCardInfo(paramsCard);
        if (cardInfos.size() > 0) {
            CardInfo card = cardInfos.get(0);
            return new Result(card.getRspCd(), card.getRspDesc(), partnerInfo.getPARTNER_MONEY_());
        }*/
        //数据库中不存在
        //logger.info("数据库中不存在卡片记录，调用四要素接口查询");
        logger.info("调用四要素接口查询");
        
        //进行添加数据到日志表中去
        CardInfoTransLog cardTranLog = new CardInfoTransLog();
        cardTranLog.setCARDNO(paramsCard.getCARD_NO_());
        if (!StringUtil.isEmpty(paramsCard.getSERIALNO_())) cardTranLog.setSERIALNO(paramsCard.getSERIALNO_());
        if (!StringUtil.isEmpty(paramsCard.getPARTNERNO_())) cardTranLog.setPARTNERNO(paramsCard.getPARTNERNO_());
        if (!StringUtil.isEmpty(paramsCard.getCARD_NAME_())) cardTranLog.setNAME(paramsCard.getCARD_NAME_());
        if (!StringUtil.isEmpty(paramsCard.getCARD_MOBILE_()))cardTranLog.setMOBILE(paramsCard.getCARD_MOBILE_());
        if (!StringUtil.isEmpty(paramsCard.getCARD_IDCARD_())) {
        	cardTranLog.setIDCARD(paramsCard.getCARD_IDCARD_());
        }
        
        //传入的第一个进入系统的时间点
       	String time1 = DateUtil.format(st1Date, DateUtil.YYYY_MM_DD_HH_MM_SS_MS);
        //调用银联后台前的时间点           
        Date st2Date = new Date();
        String time2 = DateUtil.format(st2Date, DateUtil.YYYY_MM_DD_HH_MM_SS_MS);
        //得到组装报文的时间
        long stTime = (st2Date.getTime() - st1Date.getTime());// 得到两者的毫秒数
        //将交易发起的时间添加到日志表中
        cardTranLog.setTIN(time1);
        cardTranLog.setTRANSIN(time2);
        cardTranLog.setSTTIME(stTime);
        logger.info("------------------>---交易发起的时间"+stTime);
       
        String code;
        String desc;
        logger.info("----->合作商编码" + partnerNo);
        Map<String, String> map = BankInfoCheck.Call(paramsCard.getCARD_NO_(), paramsCard.getCARD_IDCARD_(), paramsCard.getCARD_MOBILE_(), paramsCard.getCARD_NAME_());
       
            code = map.get("code");
            desc = map.get("desc");
            
         Date st3Date = new Date();
         String time3 = DateUtil.format(st3Date, DateUtil.YYYY_MM_DD_HH_MM_SS_MS);
         long trTime = (st3Date.getTime() - st2Date.getTime());// 得到两者的毫秒数
         cardTranLog.setTRANSOUT(time3);
         cardTranLog.setTRTIME(trTime);
         logger.info("------------------>---后端返回的交易时间"+trTime);
           
            
         //将返回结果插入到数据库
        paramsCard.setRspCd(code);
        paramsCard.setRspDesc(desc);
        
        //将结果插入到日志交易表中
        cardTranLog.setRspCd(code);
        cardTranLog.setRspDesc(desc);
        
        //将信息插入到交易表中
        cardInfoDao.addCardInfo(paramsCard);
     
        //返回00或者05时40时扣除消费金额
        if ("00".equals(code) || "05".equals(code) || "40".equals(code)) {
            this.transPartnerMoney(partnerInfo, Constant.BANK_PRICE, 2);
        }
        
        //返回到前端的交易时间 0914
        Date st4Date = new Date();
        String time4 = DateUtil.format(st4Date, DateUtil.YYYY_MM_DD_HH_MM_SS_MS);
        long tolTime = (st4Date.getTime() - st1Date.getTime());// 得到两者的毫秒数
        cardTranLog.setTOUT(time4);
        cardTranLog.setTOLTIME(tolTime);
        logger.info("------------------>---总交易时间"+tolTime);
        cardTransLogDao.addCardTransLog(cardTranLog);
        
        //返回结果 
        return new Result(code, desc, partnerInfo.getPARTNER_MONEY_()-Constant.BANK_PRICE);//这里在数据库更新
    }
}
